package com.guchenbo.tomcat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Component
public class ReqResServlet extends HttpServlet implements ApplicationContextAware {
    protected static final Logger log = LoggerFactory.getLogger(ReqResServlet.class);

    private ApplicationContext applicationContext;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        // 1、query string
        String queryString = req.getQueryString();
        log.info("queryString {}", queryString);
        // 2、x-www-form-urlencoded
        String fv1 = req.getParameter("fk1");
        String fv2 = req.getParameter("fk2");
        log.info("form parameter fk1 : {}, fk2 : {}", fv1, fv2);
        // 3、multipart/form-data 这个得搭配 MultipartConfigElement
        if ("multipart/form-data".equals(req.getContentType())) {
            Part part = req.getPart("ff2");
            if (part != null) {
                log.info("form file name: {}, file size: {}", part.getName(), part.getInputStream().available());
                File dest = new File("/Users/guchenbo/Downloads/" + part.getSubmittedFileName());
                StreamUtils.copy(part.getInputStream(), new FileOutputStream(dest));
            }
        }
        // 4、payload
        String payload = StreamUtils.copyToString(req.getInputStream(), StandardCharsets.UTF_8);
        log.info("payload : {}", payload);
        // 5、写入response
        InputStream inputStream = indexFile();
        byte[] buff = new byte[8192];
        resp.setContentType(MediaType.IMAGE_PNG_VALUE);
        resp.setContentLength(inputStream.available());
        int r;
        while ((r = inputStream.read(buff)) != -1) {
            resp.getOutputStream().write(buff);
            log.info("write from buff");
        }
        // 6、必须 flush
        resp.getOutputStream().flush();
    }

    public InputStream indexFile() throws IOException {
        Resource resource = applicationContext.getResource("classpath:/demo.png");
        return resource.getInputStream();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static void main(String[] args) {

        System.out.println(Integer.toHexString('\r'));
        System.out.println(Integer.toHexString('\n'));
        System.out.println(Integer.parseInt("32", 16));
        System.out.println((char) Integer.parseInt("32", 16));
        System.out.println((char) 50);

        byte[] buff = new byte[4096];
        for (int i = 0; i < buff.length; i++) {
            //            System.out.println(buff[i]);
        }

    }
}
